/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author spthanh
 */
public class Demo_JDBC_Simple1 {
    public static void main(String[] args){
        try {
            //step 0 : load driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            //step 1 : open database connectivity using JDBC
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;database=AdventureWorks", "sa", "123");
            //step 2 : create sql query
            String query = "SELECT * FROM Sales.Store WHERE [Name] LIKE 'Our%'";

            //step 3 : create Statement object
            Statement stmt = connection.createStatement();

            //step 4: execute query to return result set
            ResultSet rs = stmt.executeQuery(query);

            //step 5: process and handle data
            while(rs.next()){
                String name = rs.getString("Name");
                int salePersonId = rs.getInt("SalesPersonID");
                Date modifiedDate = rs.getDate("ModifiedDate");

                System.out.println("Name:" + name + "," + "PersonID:"
                        + salePersonId + "," + "Date:" + modifiedDate);

            }
            //step 6: close connection
            stmt.close();
            rs.close();
            connection.close();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Demo_JDBC_Simple1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }
}
